package org.kl.bpm.service.conf;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.kl.bf.utils.CRUDUtils;
import org.kl.bpm.entity.conf.BpmKvProp;
import org.kl.bpm.repository.conf.BpmKvPropDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

/**
 * 
 * @Author hqm
 * @CreateTime 2014年5月25日 下午8:40:29
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class BpmKvPropService {

	private BpmKvPropDao bpmKvPropDao;

	public BpmKvProp getBpmKvProp(String id) {
		return bpmKvPropDao.findOne(id);
	}

	public void saveBpmKvProp(BpmKvProp entity) {
		bpmKvPropDao.save(entity);
	}

	public void saveBpmKvProp(Set<BpmKvProp> entitySet) {
		bpmKvPropDao.save(entitySet);
	}

	public void deleteBpmKvProp(String id) {
		bpmKvPropDao.delete(id);
	}

	public List<BpmKvProp> findByTaskId(String taskId) {
		return bpmKvPropDao.findByTaskId(taskId);
	}

	public List<BpmKvProp> findByTaskDefinitionKeyAndBpmKvRecordRef(String taskDefinitionKey, String processInstanceId) {
		return bpmKvPropDao.findByTaskDefinitionKeyAndBpmKvRecordRef(taskDefinitionKey, processInstanceId);
	}

	public List<BpmKvProp> getAllBpmKvProp() {
		return bpmKvPropDao.findAll();
	}
	
	public List<BpmKvProp> findByRecordId(String recordId) {
		return bpmKvPropDao.findByBpmKvRecordIdOrderByCreateTimeAsc(recordId);
	}
	public String getMaxTaskId(String recordId,String taskDefinitionKey,String code){
		return bpmKvPropDao.getMaxTaskId(recordId, taskDefinitionKey, code);
	}
	public BpmKvProp findByBpmKvRecordIdAndTaskIdAndTaskDefinitionKeyAndCode(String recordId,String taskId,String taskDefinitionKey,String code){
		return bpmKvPropDao.findByBpmKvRecordIdAndTaskIdAndTaskDefinitionKeyAndCode(recordId, taskId, taskDefinitionKey, code);
	}
	public List<BpmKvProp> findByRecordIdAndPropCodeOrderByCreateTimeDesc(String recordId,String propCode) {
		return bpmKvPropDao.findByBpmKvRecordIdAndCodeOrderByCreateTimeDesc(recordId,propCode);
	}

	public Page<BpmKvProp> getUserBpmKvProp(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<BpmKvProp> spec = buildSpecification(searchParams);

		return bpmKvPropDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<BpmKvProp> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<BpmKvProp> spec = DynamicSpecifications.bySearchFilter(filters.values(), BpmKvProp.class);
		return spec;
	}

	@Autowired
	public void setBpmKvPropDao(BpmKvPropDao bpmKvPropDao) {
		this.bpmKvPropDao = bpmKvPropDao;
	}

}
